package com.xxxy.jsq.action.user;

import com.xxxy.jsq.dao.UserDao;
import com.xxxy.jsq.model.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "updateUserServlet", value = "/updateUserServlet")
public class updateUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    /**
     * @see HttpServlet#HttpServlet()
     */
    public updateUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        // 创建User对象
        User model = new User();
        // 创建UserDao对象
        UserDao dao = new UserDao();

        // 获取页面数据
        String userName = request.getParameter("userName");
        String userPwd = request.getParameter("userPwd");
        String teacherIdStr = request.getParameter("teacherId");
        String roleIdStr = request.getParameter("roleId");
        String userFlag = request.getParameter("userFlag");
        Long userId = Long.valueOf(request.getParameter("userId"));

        // 设置User对象的属性
        if (userName != null && !userName.equals("")) {
            model.setUserName(userName);
        }
        if (userPwd != null && !userPwd.equals("")) {
            model.setUserPwd(userPwd);
        }
        if (teacherIdStr != null && !teacherIdStr.equals("")) {
            model.setTeacherId(Long.valueOf(teacherIdStr));
        }
        if (roleIdStr != null && !roleIdStr.equals("")) {
            model.setRoleId(Long.valueOf(roleIdStr));
        }
        if (userFlag != null && !userFlag.equals("")) {
            model.setUserFlag(userFlag);
        }
        model.setUserId(userId);

        // 调用UserDao的update方法更新数据
        String str = dao.update(model);

        // 输出数据
        PrintWriter out = response.getWriter();
        out.print(str);
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
